package bean;


import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TimeZone;

@Data
@NoArgsConstructor
public class FileHdfsBean {
    private String uid;
    private String uid_type;
    private String agent;
    private String ip;
    private Timestamp timestamp;
    private Timestamp time;
    private String year;
    private String month;
    private String week;
    private String hour;
    private String minute;
    private String properties;

    private int game_id;
    private String timezone;
    private Date day;
    private String event;

    public FileHdfsBean(String event, String uid, String uid_type, String agent, String ip, String timestamp, String time, int game_id, String timezone) {
        this.event = event;
        this.uid = uid;
        this.uid_type = uid_type;
        this.agent = agent;
        this.ip = ip;
        timestamp = (timestamp+"0000").substring(0,13);
        time = (time+"0000").substring(0,13);
        this.game_id = game_id;
        this.timezone = timezone;
        setYearMonthWeekDayHourMin(timestamp,time);
    }

    private void setYearMonthWeekDayHourMin(String timestamp, String time) {
        long t = Long.parseLong(timestamp);
        long t2 = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf.setTimeZone(TimeZone.getTimeZone(this.timezone));
        String timestamp2 = sdf.format(t);
        String dateAndTime = sdf.format(t2);
        this.year = dateAndTime.substring(0,4);
        this.month = dateAndTime.substring(0,7);
        this.day = Date.valueOf(dateAndTime.substring(0,10));
        this.week = day +":"+ dateAndTime.substring(11,13) + getWeekOfMonth(dateAndTime.substring(0,10)) ;
        this.hour = dateAndTime.substring(0,13);
        this.minute = dateAndTime.substring(0,16);
        this.timestamp = Timestamp.valueOf(timestamp2);
        this.time = Timestamp.valueOf(dateAndTime);
    }


    public void setMap2Str(Map<String,String> map){
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String,String> entry : map.entrySet()) {
            sb.append(entry.getKey());
            sb.append(":");
            sb.append(entry.getValue());
            sb.append("&");
        }
        this.properties = sb.toString();
    }

    private String getWeekOfMonth(String day){
        int num = (Integer.parseInt(day.substring(8, 10))-1)/7+1;
        String weekOfMonth = "";
        switch(num){
            case 1 :
                weekOfMonth="(第一周)";
                break;
            case 2 :
                weekOfMonth="(第二周)";
                break;
            case 3 :
                weekOfMonth="(第三周)";
                break;
            case 4 :
                weekOfMonth="(第四周)";
                break;
            case 5 :
                weekOfMonth="(第五周)";
                break;
        }
        return weekOfMonth;
    }
}
